<template>
  <div class="article-list">
    <slot name="title"></slot>
    <div class="article-content">
      <div class="article-item" v-for="item in articleList" :key="item.id">
        <div class="item-left">
          <a
            href="javascript:;"
            class="item-tag"
            @click="handleDetails(item.path)"
            >{{ item.tag }}</a
          >
          <a
            href="javascript:;"
            class="item-img"
            @click="handleDetails(item.path, '/content-article/' + item.id)"
          >
            <img :src="item.imgHost + item.imgUrl" alt="" />
          </a>
        </div>
        <div class="item-right">
          <a
            href="javascript:;"
            class="item-title"
            @click="handleDetails(item.path, '/content-article/' + item.id)"
          >
            {{ item.title }}
          </a>
          <div class="item-introduce">
            {{ item.introduce }}
          </div>
          <div class="item-foot">
            <div class="item-foot-left">{{ item.createTime }}</div>
            <div class="item-foot-right">
              <span class="iconfont icon-dianzan1">{{ item.like_num }}</span>
              <span class="iconfont icon-Icon-guankanliang">{{
                item.look_num
              }}</span>
              <span class="iconfont icon-xiaoxi">0</span>
            </div>
          </div>
        </div>
      </div>
      <div class="article-img" v-if="articleList.length == 0">
        <img src="@/static/empty.svg" alt="" />
        <p>{{ noData }}</p>
      </div>
    </div>
    <div v-if="articleList.length != 0 && $route.path != '/category/other'">
      <div v-if="isMinScreen">
        <div class="pagination" v-if="totalPage > 1">
          <div class="pre" v-show="currentPage !== 1" @click="handlePre">
            上一页<i class="iconfont icon-you"></i>
          </div>
          <div
            class="next"
            v-show="currentPage !== totalPage"
            @click="handledNext"
          >
            下一页<i class="iconfont icon-zuo"></i>
          </div>
        </div>
      </div>
      <div v-else>
        <div class="pagination" v-if="totalPage > 1">
          <span class="total-page">{{ currentPage + " / " + totalPage }}</span>
          <div class="pre" v-show="currentPage !== 1" @click="handlePre">
            <i class="iconfont icon-you"></i>
          </div>
          <div
            class="page-num"
            v-for="item in numList"
            :key="item"
            @click="handlePage(item)"
            :style="
              currentPage == item
                ? { background: '#0088cc', color: '#fff' }
                : {}
            "
          >
            {{ item }}
          </div>
          <div
            class="next"
            v-show="currentPage !== totalPage"
            @click="handledNext"
          >
            <i class="iconfont icon-zuo"></i>
          </div>
          <div class="input-page">
            <input
              type="text"
              placeholder="页码"
              v-model="goPage"
              @keydown.enter="handleGoPage"
            />
            <i @click="handleGoPage" class="iconfont icon-youjiantou"></i>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
export default {
  name: "ArticleList",
  data() {
    return {
      path: "", //当前路径
      isMinScreen: false, //是否为小屏幕
      currentPage: 1, //当前页码
      totalPage: 1, //总页数
      numList: [], //页码列表
      articleList: [], //数据列表
      goPage: "",
      noData: "",
    };
  },
  async fetch() {
    this.currentPage = parseInt(this.$route.params.page) || this.currentPage;

    this.numList = [];
    this.path =
      this.$route.path.indexOf("/page/") != -1
        ? this.$route.path.slice(0, this.$route.path.indexOf("/page/"))
        : this.$route.path;
    let keyword = this.$route.params.keyword;
    const res = await this.$api.getArticle({
      path: this.path,
      num: 20,
      keyword,
      currentPage: this.currentPage,
    });
    this.articleList = res.data;
    this.totalPage = res.totalPage;
    // console.log(this.totalPage > 1);
    if (this.totalPage > 6 && this.currentPage > 4) {
      if (this.currentPage == this.totalPage) {
        for (let i = this.currentPage - 6; i < this.currentPage; i++) {
          this.numList.push(i + 1);
        }
      } else if (this.currentPage + 1 == this.totalPage) {
        for (let i = this.currentPage - 5; i < this.currentPage + 1; i++) {
          this.numList.push(i + 1);
        }
      } else {
        for (let i = this.currentPage - 4; i < this.currentPage + 2; i++) {
          this.numList.push(i + 1);
        }
      }
    } else {
      for (let i = 0; i < this.totalPage; i++) {
        if (i > 5) break;
        this.numList.push(i + 1);
      }
    }
    this.$cookies.set("activePage", this.currentPage);
    this.changePage(this);
    if (this.$route.name == "date") {
      // 没有归档数据
      this.noData = "该时间段没有发布文章。";
    } else if (this.$route.name == "search") {
      // 没有搜索数据
      this.noData = "请输入关键词后再进行搜索。";
    }

    this.articleList.forEach((item) => {
      this.menghuiList.forEach((citem) => {
        if (item.id == citem.id) {
          item.title = citem.title;
        }
      });
    });
  },
  mounted() {
    this.setResize();
    window.addEventListener("resize", this.setResize);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.setResize);
  },
  computed: {
    ...mapState(["menghuiList"]),
  },
  methods: {
    ...mapMutations(["changePage"]),
    ...mapActions(["handleSetActiveArr"]),
    setResize() {
      if (window.innerWidth < 768) {
        this.isMinScreen = true;
      } else {
        this.isMinScreen = false;
      }
    },
    handlePre() {
      this.currentPage--;
      this.$router.push(`${this.path}/page/${this.currentPage}`);
    },
    handledNext() {
      this.currentPage++;
      this.$router.push(`${this.path}/page/${this.currentPage}`);
    },
    handlePage(page) {
      this.currentPage = page;
      this.$router.push(`${this.path}/page/${this.currentPage}`);
    },
    handleGoPage() {
      // console.log(typeof this.goPage);
      let page = parseInt(this.goPage);
      if (typeof page != "number") {
        return;
      }
      if (page < 1 || page > this.totalPage) {
        alert("超出范围");
        this.goPage = "";
        return;
      }
      this.currentPage = page;
      this.$router.push(`${this.path}/page/${this.currentPage}`);
      this.goPage = "";
    },
    async handleDetails(path, pathId) {
      await this.handleSetActiveArr({ path, app: this });
      if (pathId) {
        this.$router.push({
          path: pathId,
        });
      } else {
        this.$router.push({
          path,
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
@media only screen and (min-width: 1240px) {
  .item-title {
    -webkit-line-clamp: 2;
  }
  .item-img {
    height: 154px;
  }
}
@media only screen and (min-width: 992px) and (max-width: 1240px) {
  .item-title {
    -webkit-line-clamp: 1;
  }
  .item-img {
    height: 124px;
  }
}
@media only screen and (max-width: 992px) {
  .item-title {
    -webkit-line-clamp: 1;
  }
  .item-img {
    height: 130px;
  }
}
@media only screen and (max-width: 768px) {
  .item-title {
    font-size: 17px !important;
    -webkit-line-clamp: 2;
  }
  .item-tag {
    display: none;
  }
  .item-img {
    height: initial;
  }
  .item-introduce {
    display: none !important;
  }
  .icon-dianzan1 {
    display: none !important;
  }
  .icon-xiaoxi {
    display: none !important;
  }
}
.article-list {
  border: 10px solid #f5f5f5;
  padding-bottom: 20px;
  background: #fff;
  .article-title {
    font-size: 22px;
    padding: 20px;
  }
  .article-content {
    padding: 0 20px 20px;
    .article-item {
      display: flex;
      padding: 20px 0;
      border-bottom: 1px solid #eee;
      .item-left {
        flex: 2;
        max-height: 154px;
        position: relative;
        overflow: hidden;
        display: flex;
        justify-content: center;
        align-items: center;
        .item-tag {
          position: absolute;
          top: 15px;
          left: 10px;
          padding: 4px 8px;
          font-size: 12px;
          border-radius: 3px;
          color: #fff;
          background: rgba(0, 0, 0, 0.6);
        }
        .item-img {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 100%;
          img {
            transition: all 0.2s;
            width: 100%;
          }
          &:hover {
            img {
              width: 110%;
            }
          }
        }
      }
      .item-right {
        flex: 4;
        padding-left: 20px;
        position: relative;
        .item-title {
          line-height: 1.5;
          color: #000;
          font-size: 20px;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          overflow: hidden;
          margin-bottom: 10px;
        }
        .item-introduce {
          line-height: 1.625;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          color: #333;
        }
        .item-foot {
          position: absolute;
          bottom: 0;
          width: 96%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 12px;
          color: #333;
          line-height: 24px;
          .iconfont {
            font-size: 12px;
            padding-left: 10px;
            &::before {
              width: 14px;
              height: 14px;
              padding-right: 2px;
            }
          }
        }
      }
    }
    .article-img {
      padding: 80px 0;
      text-align: center;
      img {
        width: 240px;
        max-width: 60%;
        margin: 20px 0;
      }
      p {
        font-size: 14px;
        color: #262626;
      }
    }
  }
  .pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    color: rgba(56, 56, 56, 0.8);
    text-align: center;
    font-size: 13px;
    span {
      height: 28px;
      line-height: 28px;
      border-radius: 2px;
      padding: 0 8px;
      margin: 0 5px;
      background: #f4f4f5;
    }
    div {
      // width: 30px;
      // height: 28px;
      line-height: 28px;
      box-sizing: border-box;
      border-radius: 2px;
      padding: 0 8px;
      margin: 0 5px;
      background: #f4f4f5;
      cursor: pointer;
      &:hover {
        color: #fff;
        background: #3361ea;
      }
    }
    .input-page {
      position: relative;
      width: 66px;
      height: 28px;
      padding: 0;
      display: flex;
      align-items: center;
      input {
        width: 66px;
        height: 28px;
        padding-left: 10px;
        border: 1px solid #eee;
        box-sizing: border-box;
        outline: none;
      }
      i {
        width: 28px;
        position: absolute;
        top: 0;
        right: 0;
        padding: 0 5px;
        box-sizing: border-box;
        color: #333;
        &:hover {
          color: #0088cc;
        }
      }
    }
  }
}
</style>